//
//  JwFile.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-01.
//

#import "JwFile.h"

@implementation JwFile

/// 判断文件存在
+ (BOOL)fileExist:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL exists = [fileManager fileExistsAtPath:path];
    return exists;
}

/// 创建文件夹
+ (BOOL)createFolder:(NSString *)path {
    if ([self fileExist:path]) {
        return YES;
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    if ([fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) {
        return YES;
    } else {
        NSLog(@"[JwFile] 创建文件夹失败%@", error);
        return NO;
    }
}

/// 删除文件夹
+ (BOOL)removeFolder:(NSString *)path {
    if ([self fileExist:path]) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error = nil;
        if (![fileManager removeItemAtPath:path error:&error]) {
            NSLog(@"[JwFile] 删除文件夹失败%@", error);
            return NO;
        }
    }
    return YES;
}

/// 创建文件
+ (BOOL)createFile:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *folderPath = [[path stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
    if (![self fileExist:folderPath]) {
        BOOL folderCreated = [self createFolder:folderPath];
        if (!folderCreated) {
            NSLog(@"[JwFile] 创建文件失败");
            return NO;
        }
    }
    if ([self fileExist:path]) {
        return YES;
    }
    NSError *error = nil;
    if (![fileManager createFileAtPath:path contents:nil attributes:nil]) {
        NSLog(@"[JwFile] 创建文件失败%@", error);
        return NO;
    }
    return YES;
}

/// 删除文件
+ (BOOL)removeFile:(NSString *)path {
    BOOL exists = [self fileExist:path];
    if (exists) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error = nil;
        if (![fileManager removeItemAtPath:path error:&error]) {
            NSLog(@"[JwFile] 删除文件失败%@", error);
            return NO;
        }
    }
    return YES;
}

/// 读取文件
+ (NSData *)readFile:(NSString *)path {
    if ([self fileExist:path]) {
        NSData *data = [NSData dataWithContentsOfFile:path];
        return data;
    } else {
        return nil;
    }
}

@end

@implementation JwFile (Write)

/// 写入文件
+ (BOOL)writeFile:(NSString *)path string:(NSString *)value {
    if (![self createFile:path]) {
        return NO;
    }
    NSError *error = nil;
    BOOL result = [value writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (!result) {
        NSLog(@"[JwFile] 写入文件失败%@", error);
        return NO;
    }
    return result;
}

/// 写入文件
+ (BOOL)writeFile:(NSString *)path array:(NSArray *)value {
    if (![self createFile:path]) {
        return NO;
    }
    BOOL result = [value writeToFile:path atomically:YES];
    if (!result) {
        NSLog(@"[JwFile] 写入文件失败");
        return NO;
    }
    return result;
}

/// 写入文件
+ (BOOL)writeFile:(NSString *)path dictionary:(NSDictionary *)value {
    if (![self createFile:path]) {
        return NO;
    }
    BOOL result = [value writeToFile:path atomically:YES];
    if (!result) {
        NSLog(@"[JwFile] 写入文件失败");
        return NO;
    }
    return result;
}

/// 写入文件
+ (BOOL)writeFile:(NSString *)path image:(UIImage *)value {
    NSData *data = UIImagePNGRepresentation(value);
    if (!data) {
        return NO;
    }
    return [self writeFile:path data:data];
}

/// 写入文件
+ (BOOL)writeFile:(NSString *)path data:(NSData *)value {
    if (![self createFile:path]) {
        return NO;
    }
    NSError *error = nil;
    BOOL result = [value writeToFile:path options:NSDataWritingAtomic error:&error];
    if (!result) {
        NSLog(@"[JwFile] 写入文件失败%@", error);
        return NO;
    }
    return result;
}

@end

@implementation JwFile (Paths)

/// Home path
+ (NSString *)homePath {
    return [NSHomeDirectory() stringByStandardizingPath];
}

/// Document path
+ (NSString *)documentsPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

/// Library path
+ (NSString *)libraryPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

/// Preferences path
+ (NSString *)preferencesPath {
    NSString *path = [self libraryPath];
    return [path stringByAppendingPathComponent:@"Preferences"];
}

/// Caches path
+ (NSString *)cachesPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

/// tmp path
+ (NSString *)tmpPath {
    NSString *path = [self homePath];
    return [path stringByAppendingPathComponent:@"tmp"];
}

@end

@implementation JwFileHandle

/// 数据写入文件
+ (BOOL)write:(NSString *)path value:(NSString *)value {
    NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding];
    return [self write:path data:data];
}

/// 数据写入文件
+ (BOOL)write:(NSString *)path data:(NSData *)data {
    if (![JwFile createFile:path]) {
        return NO;
    }
    NSError *error = nil;
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
    if (fileHandle) {
        [fileHandle seekToEndOfFile];
        [fileHandle writeData:data];
        [fileHandle synchronizeFile];
        [fileHandle closeFile];
        return YES;
    } else {
        NSLog(@"[JwFileHandle] 数据写入文件失败%@", error);
        return NO;
    }
}


@end
